<?php

namespace app\home\controller;

use app\BaseController;
use Elasticsearch\ClientBuilder;
use Es\MyElasticsearch;

class Es extends BaseController
{
    public function index()
    {
        //创建索引
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
          'index' => 'test_index'
        ];
        $r = $es->indices()->create($params);
        dump($r);die();
    }

    public function create()
    {
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
          'index' => 'test_index',
          'type' => 'test_type',
          'id' => 100,
          'body' => [
              'id' => 100,
              'title' => 'php入门到精通',
              'author' => '张三'
          ]
        ];
        $r = $es->index($params);
        dump($r);die();
    }
    public function update()
    {
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'test_index',
            'type' => 'test_type',
            'id' => 100,
            'body' => [
                'doc' => ['id'=>100,'title' => 'ES从入门到精通','author' => '张三']
            ]
        ];
        $r = $es->update($params);
        dump($r);die();
    }
    public function delete()
    {
        $es = ClientBuilder::create()->setHosts(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'test_index',
            'type' => 'test_type',
            'id' => 100
        ];
        $r = $es->delete($params);
        dump($r);die();
    }

    public function instance()
    {
        $obj = new MyElasticsearch();
        $r = $obj->existsIndex('test_index');
        halt($r);

    }
}
